package com.qianhui.dao;

import com.qianhui.common.IResult;
import com.qianhui.pojo.PreCommissionMonthRecord;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Transaction;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * A data access object (DAO) providing persistence and search support for
 * PrePreCommissionMonthRecord entities. Transaction control of the save(),
 * update() and delete() operations can directly support Spring
 * container-managed transactions or they can be augmented to handle
 * user-managed Spring transactions. Each of these methods provides additional
 * information for how to configure it for the desired type of transaction
 * control.
 *
 * @author MyEclipse Persistence Tools
 * @see com.qianhui.pojo.PrePreCommissionMonthRecord
 */
public class PreCommissionMonthRecordDAO extends BaseHibernateDAO
{
    private static final Logger log = LoggerFactory.getLogger(PreCommissionMonthRecordDAO.class);
    // property constants
    public static final String BUYER_ID = "buyerId";
    public static final String TOTAL_BUYER_NUMBER = "totalBuyerNumber";
    public static final String MONTH_ORDER_NUMBER = "monthOrderNumber";
    public static final String MONTH_GOODS_NUMBER = "monthGoodsNumber";
    public static final String MONTH_COMMISSION = "monthCommission";

    public PreCommissionMonthRecord save(PreCommissionMonthRecord transientInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().saveOrUpdate(transientInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return transientInstance;
    }

    public List<PreCommissionMonthRecord> saveList(List<PreCommissionMonthRecord> list)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            for (PreCommissionMonthRecord temp : list)
            {
                getSession().saveOrUpdate(temp);
            }
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return list;
    }

    public int delete(PreCommissionMonthRecord persistentInstance)
    {
        Transaction tx = null;
        try
        {
            tx = getSession().beginTransaction();
            getSession().delete(persistentInstance);
            tx.commit();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            if (tx != null)
            {
                tx.rollback();
            }
            return IResult.DELETE_EXCEPTION;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
        return IResult.DELETE_SUCCESS;
    }

    public PreCommissionMonthRecord findById(java.lang.String id)
    {
        log.debug("getting PreCommissionMonthRecord instance with id: " + id);
        try
        {
            PreCommissionMonthRecord instance = (PreCommissionMonthRecord) getSession().get("com.qianhui.pojo.PreCommissionMonthRecord", id);
            return instance;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByProperty(String propertyName, Object value)
    {
        log.debug("finding PreCommissionMonthRecord instance with property: " + propertyName + ", value: " + value);
        try
        {
            String queryString = "from PreCommissionMonthRecord as model where model." + propertyName + "= ?";
            Query queryObject = getSession().createQuery(queryString);
            queryObject.setParameter(0, value);
            return queryObject.list();
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List findByBuyerId(Object buyerId)
    {
        return findByProperty(BUYER_ID, buyerId);
    }

    public List findByTotalBuyerNumber(Object totalBuyerNumber)
    {
        return findByProperty(TOTAL_BUYER_NUMBER, totalBuyerNumber);
    }

    public List findByMonthOrderNumber(Object monthOrderNumber)
    {
        return findByProperty(MONTH_ORDER_NUMBER, monthOrderNumber);
    }

    public List findByMonthGoodsNumber(Object monthGoodsNumber)
    {
        return findByProperty(MONTH_GOODS_NUMBER, monthGoodsNumber);
    }

    public List findByMonthCommission(Object monthCommission)
    {
        return findByProperty(MONTH_COMMISSION, monthCommission);
    }

    public PreCommissionMonthRecord findByBuyerIdAndRecordMonth(String buyerId, Date recordMonth)
    {
        try
        {
            String sql = "from PreCommissionMonthRecord c where c.buyerId=:buyerId and c.recordMonth=:recordMonth";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("recordMonth", recordMonth);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return (PreCommissionMonthRecord) tempList.get(0);
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public List getListByBuyerIdAndIndexAndPageSizeAndBeginTimeAndEndTime(String buyerId, int index, int pageSize, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "from PreCommissionMonthRecord c where c.buyerId=:buyerId and c.recordMonth between :beginTime and :endTime order by c.recordMonth desc";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            query.setFirstResult((index - 1) * pageSize);
            query.setMaxResults(pageSize);
            List tempList = query.list();
            if (tempList == null)
            {
                return null;
            }
            else
            {
                if (tempList.size() > 0)
                {
                    return tempList;
                }
                else
                {
                    return null;
                }
            }
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return null;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }

    public int getTotalCountByBuyerIdAndBeginTimeAndEndTime(String buyerId, Date beginTime, Date endTime)
    {
        try
        {
            String sql = "select count(*) from PreCommissionMonthRecord c where c.buyerId=:buyerId and c.recordMonth between :beginTime and :endTime";
            Query query = getSession().createQuery(sql);
            query.setParameter("buyerId", buyerId);
            query.setParameter("beginTime", beginTime);
            query.setParameter("endTime", endTime);
            int num = ((Number) query.iterate().next()).intValue();
            return num;
        } catch (HibernateException e)
        {
            e.printStackTrace();
            return -1;
        } finally
        {
            if (getSession() != null && getSession().isOpen() == true)
            {
                getSession().close();
            }
        }
    }
}